package com.java2104.service;

import com.java2104.entity.RCAttendance;
import com.java2104.entity.RCClass;
import com.java2104.entity.RCUser;
import com.java2104.mapper.RCAttendanceMapper;
import com.java2104.mapper.RCClassMapper;
import com.java2104.mapper.RCUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RCAttendanceService {
    @Autowired
    private RCAttendanceMapper attendanceMapper;
    @Autowired
    private RCClassMapper classMapper;
    @Autowired
    private RCUserMapper userMapper;

    public boolean save(RCAttendance attendance){
    //1.在出勤表中添加一条记录
    int studentNum=attendance.getAttendancePersonNum()+attendance.getAbsencePersonNum();
    //计算出勤率=出勤人数/总人数
    float atendanceRate=attendance.getAttendancePersonNum()/(float)studentNum;
    attendance.setAttendanceRate(atendanceRate);
    attendanceMapper.insert(attendance);
    //2.修改班级的点名次数，累计缺勤人数和累计缺勤率
        RCClass rcClass=classMapper.findByPrimaryKey(attendance.getRcClass().getId());
        rcClass.setCheckNum(rcClass.getCheckNum()+1);//点名次数
        //累计缺勤人数=历史缺勤人数+本次缺勤人数
        rcClass.setAbsencePersonNum(rcClass.getAbsencePersonNum()+attendance.getAbsencePersonNum());
        //计算累计缺勤率
        rcClass.setAbsenceRate(rcClass.getAbsenceRate()+(1-atendanceRate));
        //更新班级的缺勤信息
        classMapper.update(rcClass);
    //3.修改点名人的点名次数
        RCUser rcUser=userMapper.foreignkey(attendance.getRcUser().getId());
        rcUser.setCheckNum(rcUser.getCheckNum()+1);
        userMapper.update(rcUser);
    return  false;
    }
    public List<RCAttendance> getList(int classId,int userId){
        return attendanceMapper.findList(classId,userId);
    }
}
